import { Repository } from 'typeorm';
import { Body, Controller, Param, Post } from '@midwayjs/core';
import { Account } from '../entity/account.entity';
import { InjectEntityModel } from '@midwayjs/typeorm';

@Controller('/account')
export class AccountController {
  @InjectEntityModel(Account)
  repo: Repository<Account>;
  @Post('/insert')
  async insert(@Body() account: Account) {
    account.accountId = 0;
    return await this.repo.save(account);
  
  }
  @Post('/query/:pageNum')
  async query(@Param('pageNum') pageNum: number) {
    const pageSize = 10;
    const pageStart = (pageNum - 1) * pageSize;
    const builder = this.repo.createQueryBuilder('a');
    builder.offset(pageStart).limit(pageSize);
    const r = await builder.getManyAndCount();
    return {
      results: r[0],
      total: r[1],
    };
  }
}
